package com.gywj.race.config.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * swagger2定义
 */
@Configuration
@EnableSwagger2
public class SwaggerApplication {

    @Value("${spring.application.swagger.enableSwagger:true}")
    private boolean enableSwagger;
    @Value("${spring.application.swagger.enableAdmin:false}")
    private boolean enableAdmin;

    /**
     * swagger2
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .directModelSubstitute(LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(LocalDateTime.class, java.util.Date.class)
                .select()
                //为当前包路径
                .apis(requestHandler -> {
                    assert requestHandler != null;
                    String packageName = requestHandler.getHandlerMethod().getMethod()
                            .getDeclaringClass().getPackage().getName();
                    if (!enableAdmin && packageName.contains("com.jiayuan")) {
                        return false;
                    } else {
                        return packageName.contains("controller");
                    }
                })
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("戈友会体育赛事平台基于Swagger2构建Restful APIs Document")
                .description("红戈友会体育赛事平台平台接口文档")
                .termsOfServiceUrl("http://www.geyuewujiang.com")
                .version("1.0").build();
    }
}
